package com.zby.simulationHibernate.util.common;

import org.apache.commons.lang3.StringUtils;

/**
 * Created By zby on 10:32 2019/1/29
 * 处理String数组的工具类
 */

public class StringUtil {


    /**
     * Created By zby on 15:55 2019/1/29
     * 首字母小写
     */
    public static String firstLetterToLower(String param) {
        return letterToLower(param, 0, 1);
    }

    /**
     * Created By zby on 15:57 2019/1/29
     * 某些字母转小写
     *
     * @param param 字符串参数
     * @param start 开始位置
     * @param end   结束位置
     */
    public static String letterToLower(String param, int start, int end) {

        if (CommonUtil.isNull(param))
            throw new IllegalArgumentException("参数为空param=" + param);
        if (start < 0 || end < 0)
            throw new IllegalArgumentException("开始位置或结束位置不能为空");
        if (start >= end)
            throw new IllegalArgumentException("开始位置不能小于结束位置start=" + start + ",end=" + end);
        if (end > param.length())
            throw new IllegalArgumentException("结束位置越界，end=" + end);
        return param.substring(0, start) + param.substring(start, end).toLowerCase() + param.substring(end, param.length());
    }

    /**
     * Created By zby on 15:27 2019/1/30
     * 替换字符
     */
    public static StringBuilder replace(StringBuilder stringBuilder, String ch) {
        if (CommonUtil.isNull(stringBuilder))
            throw new IllegalArgumentException("需要被替换的字符为空【" + stringBuilder + "】");
        if (CommonUtil.isNull(ch))
            throw new IllegalArgumentException("替换的字符【" + ch + "】");
        if (StringUtils.contains(stringBuilder, ch))
            return stringBuilder.replace(stringBuilder.lastIndexOf(ch), stringBuilder.length(), " ");
        return stringBuilder;
    }

}
